package domaine



import collection.mutable.HashMap
import scala.io.Source


object StatusReader {


  private val table_symbs : HashMap [String,String]= new HashMap[String,String]

  def ReadStatus(path:String)={

   table_symbs.clear()

   var result = new ProcessStatus(0)
   
   val lines  = Source.fromFile(path).getLines.toList
    
   lines.foreach(line=>
    {
      val tokens =line.replaceAll("kB","").split(":")
      //Console.println("Key {"+tokens(0)+","+tokens(1)+"}")
    	
      try{
          table_symbs+=(tokens(0)->tokens(1))
      }
      catch{
        case e:Exception => //println("Invalid process file")
      }
      
    })
    result.VmData =table_symbs.getOrElseUpdate("VmData","0").replace(" ","").replace("\t","").toInt
    result.VmExe  =table_symbs.getOrElseUpdate("VmExe","0").replace(" ","").replace("\t","").toInt
    result.VmHWM  =table_symbs.getOrElseUpdate("VmHWM","0").replace(" ","").replace("\t","").toInt
    result.VmLib  =table_symbs.getOrElseUpdate("VmLib","0").replace(" ","").replace("\t","").toInt
    result.VmLock =table_symbs.getOrElseUpdate("VmLock","0").replace(" ","").replace("\t","").toInt
    result.VmPeak =table_symbs.getOrElseUpdate("VmPeak","0").replace(" ","").replace("\t","").toInt
    result.VmPTE  =table_symbs.getOrElseUpdate("VmPTE","0").replace(" ","").replace("\t","").toInt
    result.VmRSS  =table_symbs.getOrElseUpdate("VmRSS","0").replace(" ","").replace("\t","").toInt
    result.VmStk  =table_symbs.getOrElseUpdate("VmStk","0").replace(" ","").replace("\t","").toInt
    result.VmSwap =table_symbs.getOrElseUpdate("VmSwap","0").replace(" ","").replace("\t","").toInt
    result.Threads=table_symbs.getOrElseUpdate("Threads","0").replace(" ","").replace("\t","").toInt
    result.Pid    =table_symbs.getOrElseUpdate("Pid","0").replace(" ","").replace("\t","").toInt
    result.PPid   =table_symbs.getOrElseUpdate("PPid","0").replace(" ","").replace("\t","").toInt
    result.Name   =table_symbs.getOrElseUpdate("Name","0").replace(" ","").replace("\t","")
    result.State  =table_symbs.getOrElseUpdate("State","0").replace(" ","").replace("\t","")
    
    result
  }
}